package com.zhuge_new.Adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhuge_new.R;
import com.zhuge_new.bean.IndirectBean;
import com.zhuge_new.untils.DataUtilS;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.RemoveDuplicateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/10/16.
 */

public class IndiectAdapter extends BaseQuickAdapter<IndirectBean.LsBean,BaseViewHolder> {


    private List<IndirectBean.LsBean> data = new ArrayList<>();
    private Context context;
    private String curre;
    private ArrayList<String> listnum=new ArrayList();
    private ArrayList<String> listmoney=new ArrayList();
    private double total_money;
    private Double conversion;
    private ArrayList list_subscript=new ArrayList();

    public IndiectAdapter(Context context, @Nullable List<IndirectBean.LsBean> data) {
        super(R.layout.item_direct, data);
        this.data = data;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        Log.e("查看是否赋值",data.get(position).getCreateDate());
        int month = DataUtilS.getMonth();
        if (month < 10) {
            curre = "0" + String.valueOf(month);
        }else {
            curre=String.valueOf(month);
        }
        listnum.clear();
        listmoney.clear();
        for (int i=0;i<data.size();i++){
            listnum.add(data.get(i).getCreateDate().substring(5,7));
        }
        RemoveDuplicateUtil.removeDuplicate(listnum);
        String datamonth=listnum.get(position);
        switch (datamonth){
            case "01":
                holder.setText(R.id.direct_month,"一月");
                break;
            case "02":
                holder.setText(R.id.direct_month,"二月");
                break;
            case "03":
                holder.setText(R.id.direct_month,"三月");
                break;
            case "04":
                holder.setText(R.id.direct_month,"四月");
                break;
            case "05":
                holder.setText(R.id.direct_month,"五月");
                break;
            case "06":
                holder.setText(R.id.direct_month,"六月");
                break;
            case "07":
                holder.setText(R.id.direct_month,"七月");
                break;
            case "08":
                holder.setText(R.id.direct_month,"八月");
                break;
            case "09":
                holder.setText(R.id.direct_month,"九月");
                break;
            case "10":
                holder.setText(R.id.direct_month,"十月");
                break;
            case "11":
                holder.setText(R.id.direct_month,"十一月");
                break;
            case "12":
                holder.setText(R.id.direct_month,"十二月");
                break;
        }
        if (datamonth.equals(curre)){
            holder.setText(R.id.direct_month,"本月");
        }
        list_subscript.clear();
        for (int i=0;i<data.size();i++){
            if (datamonth.equals(data.get(i).getCreateDate().substring(5,7))){
                listmoney.add(data.get(i).getProfitSum());
                list_subscript.add(i);
            }

        }
        holder.setText(R.id.direct_quantity,"共"+String.valueOf(listmoney.size())+"人");
        for (int i = 0; i<listmoney.size(); i++){

            if (listmoney.get(i).isEmpty()){
                conversion = Double.valueOf(0);
            }else {
                conversion=Double.valueOf(listmoney.get(i));
            }
            total_money=total_money+conversion;
        }
        holder.setText(R.id.direct_earnings,"总收益￥"+ NumberUtil.saveOneBitTwoRound(total_money));
        total_money=0;
        SwipeMenuRecyclerView right_list=holder.getView(R.id.right_list);
        right_list.setLayoutManager(new LinearLayoutManager(context));
        IndiectSonAdapter indiectSonAdapter=new IndiectSonAdapter(context,data,listnum.get(position),list_subscript);
        indiectSonAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        right_list.setSwipeMenuCreator(swipeMenuCreator);
        right_list.setSwipeMenuItemClickListener(mMenuItemClickListener);
        right_list.setAdapter(indiectSonAdapter);
    }

    @Override
    public int getItemCount() {
        listnum.clear();
        for (int i=0;i<data.size();i++){
            listnum.add(data.get(i).getCreateDate().substring(5,7));
        }
        RemoveDuplicateUtil.removeDuplicate(listnum);
        Log.e(listnum.toString(),"打印第一条目");
        return listnum.size();
    }

    @Override
    protected void convert(BaseViewHolder helper, IndirectBean.LsBean item) {
        helper.setText(R.id.direct_month,"十一月");
    }


    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = 70;


            // 1. MATCH_PARENT 自适应高度，保持和Item一样高;
            // 2. 指定具体的高，比如80;
            // 3. WRAP_CONTENT，自身高度，不推荐;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;

            // 添加右侧的，如果不添加，则右侧不会出现菜单。
            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                        .setBackground(R.color.btn_backround)
                        .setImage(R.mipmap.ic_launcher)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加菜单到右侧。

                SwipeMenuItem addItem = new SwipeMenuItem(context)
                        .setBackground(R.color.btn_backround)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(addItem); // 添加菜单到右侧。
            }
        }
    };


    //点击监听

    private SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            menuBridge.closeMenu();

            int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
            int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
            int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。

            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                String number = data.get(adapterPosition).getMobile();
                call(number);
            } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {//左

            }
        }
    };

    private void call(String number) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
